using CriWare;
using UnityEngine;

namespace IQIGame.Onigao.Game
{
    public class VideoPlayerForUI : VideoPlayerBase
    {

        protected override CriManaMovieMaterial CreateRawPlayer(Transform root, bool applyTargetAlpha, bool additiveMode)
        {
            var go = new GameObject(nameof(_rawVideoPlayer));
            go.transform.SetParent(root);
            go.SetActive(false);
            _rawVideoPlayer = go.AddComponent<CriManaMovieControllerForUI>();
            _rawVideoPlayer.applyTargetAlpha = applyTargetAlpha;
            _rawVideoPlayer.additiveMode = additiveMode;
            go.SetActive(true);
            return _rawVideoPlayer;
        }


        /// <summary>
        /// 后续重载
        /// </summary>
        /// <param name="rimg"></param>
        public void SetVideoTarget(UnityEngine.UI.Graphic uiGraphic)
        {
            (_rawVideoPlayer as CriManaMovieControllerForUI).target = uiGraphic;
            if (uiGraphic != null)
            {
                _rawVideoPlayer.enabled = true;
            }
        }

        protected override bool CheckVideoTarget()
        {
            var uiVideoPlayer = (_rawVideoPlayer as CriManaMovieControllerForUI);
            if (uiVideoPlayer != null)
            {
                return uiVideoPlayer.target != null;
            }
            return false;
        }

        public override void OnReset()
        {
            var uiVideoPlayer = (_rawVideoPlayer as CriManaMovieControllerForUI);
            if (uiVideoPlayer != null)
            {
                if (uiVideoPlayer.target != null)
                {
                    var color = uiVideoPlayer.target.color;
                    color.a = 1;
                    uiVideoPlayer.target.color = color;
                    uiVideoPlayer.target = null;
                }
            }

            base.OnReset();
        }

    }
}
